package com.taptech.doufu.ui.view.drawcircle;

import android.content.Context;
import android.os.Handler;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.viewholder.personalcenter.AlbumsRecyclerViewHolder;
import com.taptech.doufu.ui.viewholder.personalcenter.CartoonVerticalViewHolder;
import com.taptech.doufu.ui.viewholder.personalcenter.MyNovelSectionDraftRecyclerViewHolder;
import com.taptech.doufu.ui.viewholder.personalcenter.MyNovelSectionRecyclerViewHolder;
import com.taptech.doufu.ui.viewholder.personalcenter.NullRecyclerViewHolder;
import com.taptech.doufu.ui.viewholder.personalcenter.SweepRecyclerViewHolder;
import com.taptech.doufu.ui.viewholder.personalcenter.UserRecyclerViewHolder;
import com.taptech.doufu.ui.viewholder.personalcenter.PersonalNoteViewHolder;
import com.taptech.doufu.ui.viewholder.TagCollectViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lsy on 2015/8/3.
 */
public class RecyclerViewAdapterAsListView<T extends MineAbstractBean> extends RecyclerView.Adapter<BaseRecyclerViewViewHolder>
{

    // headerView的类型标志
    final private int HEADER_VIEW_TYPE_FLAG = 1000;
    // footerView的类型标志
    final private int FOOTER_VIEW_TYPE_FLAG = 2000;
    final private int FOOTER_LOAD_MORE_FLAG = 3000;
    private boolean isHeadViewVisible = false;
    private Context mContext;
    private Handler mHandler;
    private List<Integer> mHeaderViewHolderIdList;
    private List<Integer> mFooterViewHolderIdList;
    private List<View> mHeaderViewList;
    private List<View> mFooterViewList;
    private List<T> mDataList;
    private boolean pullMoreComplete = false;
    private boolean isFooterVisible = true;
    private int loadMoreLayoutId = 0;
    private ArrayList<String> novelIDs;
    private int type;
    public static final int TYPE_DELETE_FROM_PERSONAL = 111;
    public static final int TYPE_DELETE_FROM_COLLECT = TYPE_DELETE_FROM_PERSONAL + 1;
    private int coverType;

    public RecyclerViewAdapterAsListView(Context context, List<T> dataList)
    {
        initView(context, dataList);
    }

    public RecyclerViewAdapterAsListView(Context context, List<T> dataList, int coverType) {
        this.coverType = coverType;
        initView(context, dataList);
    }

    public RecyclerViewAdapterAsListView(Context context, List<T> dataList, Handler handler)
    {
        initView(context, dataList);
        this.mHandler = handler;
    }

    private void initView(Context context, List<T> dataList)
    {
        this.mContext = context;
        this.mDataList = dataList;

        mHeaderViewHolderIdList = new ArrayList<Integer>();

        mFooterViewHolderIdList = new ArrayList<Integer>();
        mHeaderViewList = new ArrayList<View>();
        mFooterViewList = new ArrayList<View>();
    }

    @Override
    public int getItemViewType(int position)
    {
        int itemType = 0;

        if (position < mHeaderViewList.size())
        {
            itemType = HEADER_VIEW_TYPE_FLAG + position;
        } else if (position == mHeaderViewList.size() + mDataList.size())
        {
            itemType = FOOTER_LOAD_MORE_FLAG;
        } else
        {
            String object_type = mDataList.get(position - mHeaderViewList.size()).getObject_type();
            TTLog.i("TAG1", "object_type:" + object_type);
            // 话题、小说、视频Object同为5，要单独处理
            if (object_type != null)
            {
                itemType = Integer.valueOf(object_type);
            }
            if (object_type != null && object_type.equals("5"))
            {

                String topic_type = mDataList.get(position - mHeaderViewList.size()).getTopic_type();
                if ("16".equals(topic_type))
                {
                    return Constant.OBJECT_TYPE_USERS_TOPIC;
                } else if ("18".equals(topic_type))
                {
                    return Constant.OBJECT_TYPE_NOVEL;
                }
            } else
            {


            }

        }

        return itemType;
    }

    @Override
    public BaseRecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {

        // 根据viewType获得viewHolder实例
        BaseRecyclerViewViewHolder viewHolder = new ViewHolderFactory().createViewHolderByType(mContext, viewType);
        if (viewHolder != null && mHandler != null)
        {
            viewHolder.setHandler(mHandler);
        }
        viewHolder.setDeleteType(type);
        // itemView为headerView
        if (viewType / HEADER_VIEW_TYPE_FLAG == 1)
        {
            View headerView = mHeaderViewList.get(viewType % HEADER_VIEW_TYPE_FLAG);
            viewHolder = new FooterViewHolder(mContext, headerView);
        }
        // itemView为footerView
        else if (viewType / HEADER_VIEW_TYPE_FLAG == 2)
        {
            View footerView = LayoutInflater.from(mContext).inflate(mFooterViewHolderIdList.get(viewType % FOOTER_VIEW_TYPE_FLAG), parent, false);
            viewHolder = new FooterViewHolder(mContext, footerView);
        } else if (viewType == FOOTER_LOAD_MORE_FLAG)
        {
            View footerView = null;
            if (loadMoreLayoutId == 0)
            {
                footerView = LayoutInflater.from(mContext).inflate(R.layout.recyclerview_pull_headview, parent, false);
            } else
            {
                footerView = LayoutInflater.from(mContext).inflate(loadMoreLayoutId, parent, false);
            }

            viewHolder = new FooterViewHolder(mContext, footerView);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewViewHolder holder, int position)
    {
        try
        {
            if (isContentPosition(position))
            {
                if (mHeaderViewList != null)
                {
                    holder.setViewHolderChildViewContent(mDataList.get(position - mHeaderViewList.size()), position);
                } else
                {
                    holder.setViewHolderChildViewContent(mDataList.get(position), position);
                }

            }
            if (position == mHeaderViewList.size() + mDataList.size())
            {
                holder.setViewHolderChildViewContent(null, position);

            }

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    @Override
    public int getItemCount()
    {
        int count = 0;

        int headAndFootSize = mHeaderViewList.size() + mFooterViewHolderIdList.size();
        if (mDataList != null)
        {
            count = headAndFootSize + mDataList.size();

        } else
        {
            count = headAndFootSize;
        }

        count++;

        return count;
    }

    public void setType(int type)
    {
        this.type = type;
    }

    class ViewHolderFactory
    {
        View view = null;

        public BaseRecyclerViewViewHolder createViewHolderByType(Context context, int viewType)
        {
            BaseRecyclerViewViewHolder viewHolder = null;
            View view = null;
            switch (viewType)
            {
                case Constant.OBJECT_TYPE_NOVEL:
                    view = LayoutInflater.from(context).inflate(R.layout.mine_novel_item_layout, null);
                    if (view != null)
                    {
                        viewHolder = new NovelRecyclerViewViewHolder(mContext, view);
                    }
                    break;
                case Constant.OBJECT_TYPE_USERS_TOPIC:
                    view = LayoutInflater.from(context).inflate(R.layout.home_item_subject, null);
                    if (view != null)
                    {
                        viewHolder = new TopicRecyclerViewHolder(mContext, view);
                    }
                    break;
                case Constant.OBJECT_TYPE_SWEEP:
                    view = LayoutInflater.from(context).inflate(R.layout.home_item_sweep_new, null);
                    if (view != null)
                    {
                        viewHolder = new SweepRecyclerViewHolder(mContext, view);
                    }
                    break;
                case Constant.OBJECT_TYPE_NOTE:
                    view = LayoutInflater.from(context).inflate(R.layout.personal_note_viewholder_layout, null);
                    if (view != null)
                    {
                        viewHolder = new PersonalNoteViewHolder(view, mContext);
                    }
                    break;
                case Constant.OBJECT_TYPE_DRAW:
                    view = LayoutInflater.from(context).inflate(R.layout.personal_note_viewholder_layout, null);
                    if (view != null)
                    {
                        viewHolder = new PersonalNoteViewHolder(view, mContext);
                    }
                    break;
                case Constant.OBJECT_TYPE_COS:
                    view = LayoutInflater.from(context).inflate(R.layout.personal_note_viewholder_layout, null);
                    if (view != null)
                    {
                        viewHolder = new PersonalNoteViewHolder(view, mContext);
                    }
                    break;

                case Constant.OBJECT_TYPE_TAG_COLLECT:
                    view = LayoutInflater.from(context).inflate(R.layout.tag_collect_item_layout, null);
                    if (view != null)
                    {
                        viewHolder = new TagCollectViewHolder(mContext, view);
                        ((TagCollectViewHolder)viewHolder).setType(type);
                    }
                    break;
                case Constant.CONTENT_TYPE_USER:
                    view = LayoutInflater.from(context).inflate(R.layout.albums_user_item_layout, null);
                    if (view != null)
                    {
                        viewHolder = new UserRecyclerViewHolder(mContext, view);
                    }
                    break;
                case Constant.OBJECT_TYPE_ALBUMS:
                    view = LayoutInflater.from(context).inflate(R.layout.albums_item_viewholder_layout, null);
                    if (view != null)
                    {
                        viewHolder = new AlbumsRecyclerViewHolder(mContext, view);
                    }
                    break;
                // 这是小说章节
                case Constant.CONTENT_TYPE_WORDS:
                    view = LayoutInflater.from(context).inflate(R.layout.item_novel_create_list, null);
                    if (view != null)
                    {
                        viewHolder = new MyNovelSectionRecyclerViewHolder(mContext, view);
                    }
                    break;
                // 这是小说章节草稿
                case Constant.OBJECT_TYPE_NOVEL_SECTION_DRAFT:
                    view = LayoutInflater.from(context).inflate(R.layout.item_novel_create_draft, null);
                    if (view != null)
                    {
                        viewHolder = new MyNovelSectionDraftRecyclerViewHolder(mContext, view, novelIDs);
                    }
                    break;
                case Constant.OBJECT_TYPE_CARTOON:
                    view = LayoutInflater.from(context).inflate(R.layout.cartoon_vertical_cover_item, null);
                    if (view != null) {
                        viewHolder = new CartoonVerticalViewHolder(mContext, view);
                    }
                    break;


                default:
                    view = LayoutInflater.from(context).inflate(R.layout.null_item_layout, null);
                    if (view != null)
                    {
                        viewHolder = new NullRecyclerViewHolder(mContext, view);
                    }
                    break;
            }
            return viewHolder;
        }
    }

    public void setNovelIDs(ArrayList<String> novelIDs)
    {
        this.novelIDs = novelIDs;
    }


    public void addHeaderView(int headViewId)
    {
        mHeaderViewHolderIdList.add(headViewId);
    }

    public void addHeaderView(View view)
    {
        mHeaderViewList.add(view);
    }

    public void addHeaderView(View view, int position)
    {
        mHeaderViewList.add(position, view);
    }




    private class FooterViewHolder extends BaseRecyclerViewViewHolder
    {

        View progressBar;
        View noMoreImageView;

        public FooterViewHolder(Context context, View itemView)
        {
            super(context, itemView);
            progressBar = itemView.findViewById(R.id.xlistview_footer_progressbar);
            noMoreImageView = itemView.findViewById(R.id.no_more_content);

        }

        @Override
        public void setViewHolderChildViewContent(MineAbstractBean bean, int position)
        {
            if(!isFooterVisible){
                noMoreImageView.setVisibility(View.INVISIBLE);
                progressBar.setVisibility(View.INVISIBLE);
                return;
            }
            if (pullMoreComplete)
            {
                noMoreImageView.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.GONE);
            } else
            {
                noMoreImageView.setVisibility(View.GONE);
                progressBar.setVisibility(View.VISIBLE);
            }
        }
    }

    private boolean isContentPosition(int position)
    {
        boolean result = false;
        if (position < mHeaderViewList.size())
        {
            result = false;
        } else if (position >= mHeaderViewList.size() + mDataList.size())
        {
            result = false;
        } else
        {
            result = true;
        }
        return result;
    }

    public void pullComplete(boolean pullMoreComplete)
    {
        this.pullMoreComplete = pullMoreComplete;
        this.notifyDataSetChanged();
    }


    public boolean isHeadViewVisible()
    {
        return isHeadViewVisible;
    }

    public void setFooterVisible(boolean isTrue){
        isFooterVisible = isTrue;
    }

    public void setHeadViewVisible(int isVisible)
    {
        if (isVisible == View.GONE && mDataList != null && mDataList.size() > 0)
        {
            /*mDataList.remove(0);
            notifyItemRemoved(0);*/
            isHeadViewVisible = false;
        } else if (isVisible == View.VISIBLE)
        {
            if (mDataList != null)
            {
                // mDataList.add(0, new T<>);
                isHeadViewVisible = true;
                // notifyItemInserted(0);
            }
        }
    }
}
